let FD = (function () {
  function FD () {}
  // From表单验证规则  可用于公用的校验部分
  FD.Fd = function () {
    Date.prototype.format = function (format) {
      var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
      }

      if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
      return format;
    }
    return {
      initCNDateFromlong(dateLong){
        var date = new Date(dateLong);
        var theDate = date.toLocaleString();

        var theDdate1 = theDate.replace(/年/,'-')
        var theDdate2 = theDdate1 .replace(/月/,'-')
        var theDdate3 = theDdate2 .replace(/日/,'')
        var year = theDdate3.substring(0,4);
        var theDate4 = theDdate3.substring(5);
        var monthIndex = theDate4.indexOf("/");
        var initMonth = theDate4.substring(0,monthIndex);
        var month = initMonth;
        if(initMonth.length ==1){
          month = '0'+ initMonth;
        }

        var theDate5 = theDate4.substring(monthIndex);
        var dayIndex = theDate5.indexOf(" ");
        var initDay = theDate5.substring(1,dayIndex);
        var day = initDay;
        if(initDay.length ==1){
          day = '0'+ initDay;
        }
        var resultDay = year + '-' + month + '-' + day;
        return resultDay;
      },
      initCNDate(date){
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h =date.getHours()
        h = h < 10 ? ('0' + h) : h
        let M =date.getMinutes()
        M = M < 10 ? ('0' + M) : M
        let s =date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        let initdate= y + '-' + m + '-' + d;
        return initdate;
      },
      initCNTime(date){
        let y = date.getFullYear()
        let m = date.getMonth() + 1
        m = m < 10 ? ('0' + m) : m
        let d = date.getDate()
        d = d < 10 ? ('0' + d) : d
        let h =date.getHours()
        h = h < 10 ? ('0' + h) : h
        let M =date.getMinutes()
        M = M < 10 ? ('0' + M) : M
        let s =date.getSeconds()
        s = s < 10 ? ('0' + s) : s
        let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
        return dateTime;
      },
      dateFormat(row) {
        var t = new Date(row.createTime)// row 表示一行数据, createTime 表示要格式化的字段名称
        if (!t) {
          return ''
        }
        let year = t.getFullYear()
        let month = this.dateIfAddZero(t.getMonth() + 1)
        let day = this.dateIfAddZero(t.getDate())
        let hours = this.dateIfAddZero(t.getHours())
        let minutes = this.dateIfAddZero(t.getMinutes())
        let seconds = this.dateIfAddZero(t.getSeconds())
        return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
      },
      add0(m) {
        return m < 10 ? '0' + m : m
      },
      format(date) {
        //shijianchuo是整数，否则要parseInt转换
        var time = new Date(date);
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getUTCMinutes();
        var s = time.getUTCSeconds() ;
        return y + '-' + this.add0(m) + '-' + this.add0(d) + ' ' + this.add0(h) + ':' + this.add0(mm) + ':' + this.add0(s);
      },

      dateIfAddZero(time) {
        return time < 10 ? '0' + time : time
      },




       longDateFormat(longTypeDate)
        {
         function add0(m){return m<10?'0'+m:m }
          //longTypeDate，否则要parseInt转换
          var time = new Date(longTypeDate);
          var y = time.getFullYear();
          var m = time.getMonth()+1;
          var d = time.getDate();
          var h = time.getHours();
          var mm = time.getMinutes();
          var s = time.getSeconds();
          return y+'-'+add0(m)+'-'+add0(d);
        },
      //求自然月日期
      getMonthBeforeFormatAndDay(num, format, day) {
      var date = new Date();
      date.setMonth(date.getMonth() + (num*1), 1);
      //读取日期自动会减一，所以要加一
      var mo = date.getMonth() + 1;
      //小月
      if (mo == 4 || mo == 6 || mo == 9 || mo == 11) {
        if (day > 30) {
          day = 30
        }
      }
      //2月
      else if (mo == 2) {
        if (isLeapYear(date.getFullYear())) {
          if (day > 29) {
            day = 29
          } else {
            day = 28
          }
        }
        if (day > 28) {
          day = 28
        }
      }
      //大月
      else {
        if (day > 31) {
          day = 31
        }
      }

      retureValue = date.format('yyyy' + format + 'MM' + format + day);

      return retureValue;
    },

    //JS判断闰年代码
     isLeapYear(Year) {
      if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
      } else { return (false); }
    },


    }

  }

  return FD
}())

exports.FD = FD

